package noUsed;

import general.Case;

import java.util.ArrayList;

public class OldFluidSystem
{
	Case[][] g;
	int taille = 10;
	private ArrayList<Case>	WC = new ArrayList<Case>();
	private float	borneMin = (float) 0.005;
	
	public OldFluidSystem( )
	{
		g = new Case[taille][taille];
		
		for( int i = 0 ; i < taille ; i++ )
		{
			for( int j = 0 ; j < taille ; j++ )
			{
				g[i][j] = new Case( 0 , i, j );
			}
		}
	}
	
	public void presenter()
	{
		for( int i = 0 ; i < taille ; i++ )
		{
			for( int j = 0 ; j < taille ; j++ )
			{
				System.out.print(" " + ef(g[i][j].vf) + "/" + ef(g[i][j].alt) );
			}
			System.out.println();
		}
	}
	
	public String ef( float n )
	{
		if( n < 0.0001 )
			return ("0.0000000000").substring(0, 6);
		
		return (n + "0000000000").substring(0, 6);
	}
	
	public static void main(String[] args)
	{
		OldFluidSystem fs = new OldFluidSystem();
		
		fs.sourcer( 0 , 0 , 8 );
		
		fs.presenter();
		
		for( int i = 0 ; i < 60 ; i++ )
		{
			System.out.println("\nIteration : " + i );
			fs.sourcer( 0 , 0 , 8 );
			fs.avancer();
			fs.presenter();
			
			System.out.println("Size CW : " + fs.WC.size() );
			fs.presenterListe();
			System.out.println( "Total : " + fs.total() );
		}
	}

	private float total()
	{
		float sum = 0;
		for( int i = 0 ; i < WC.size() ; i++ )
		{
			sum  +=  WC.get(i).vf;
		}
		return sum;
	}

	private void presenterListe()
	{
		System.out.println();
		for( int i = 0 ; i < WC.size() ; i++ )
		{
			System.out.print(" " + ef(WC.get(i).vf) );
		}
		System.out.println();
	}

	private void sourcer(int i, int j, int vf)
	{
		g[i][j].vf = vf;
		
		if( !WC.contains( g[i][j] ))
			WC.add( g[i][j] );
	}

	private void avancer()
	{
		int x;
		int y;
		
		ArrayList<Case>	newWC = new ArrayList<Case>();
		ArrayList<Case>	lWCl;
		float sum = 0;
		float sumV = 0;
		float dimi;
		
		for( int n = 0 ; n < WC.size() ; n++ )
		{
			lWCl = new ArrayList<Case>();
			sum = 0;
			sumV = 0;
			//System.out.println("\nPoint : " + WC.get(n).vf );
			
			for( int i = -1 ; i < 2 ; i++ )
			{
				for( int j = -1 ; j < 2 ; j++ )
				{
					x = WC.get(n).x + i;
					y = WC.get(n).y + j;
					
					if( x >= 0 && x < taille && y >= 0 && y < taille )
					{
						float dif = -(g[x][y].alt + g[x][y].vf) + (WC.get(n).alt + WC.get(n).vf );
						
						if( dif > 0 )
						{
							g[x][y].dif = dif;
							lWCl.add( g[x][y] );
							sum += dif;
						}
						
						sumV++;
					}
				}
			}
			
			if( lWCl.size() != 0 )
			{
				//System.out.println("Avant " + sum);
				float sumT = sum;
				sum = sum /sumV;
				//System.out.println("Apres " + sum);
				
				if( sum > WC.get(n).vf )
				{
					dimi = sum / WC.get(n).vf;
				}
				else
				{
					dimi = 1;
				}
				
				//System.out.println("Dimi " + dimi);
				
				if( sum > borneMin  )
				{
					for( int i = 0 ; i < lWCl.size() ; i++ )
					{
						float coef = lWCl.get(i).dif/sumT;
						//System.out.println("Coef : " + coef);
						lWCl.get(i).courant += sum * coef / dimi;
						
						if( !newWC.contains( lWCl.get(i) ))
						newWC.add( lWCl.get(i) );
					}
					
					//System.out.println("Sum/dimi" + (sum / dimi) );
					WC.get(n).courant += WC.get(n).vf - sum / dimi;

					if( WC.get(n).vf > borneMin )
					{
						
						if( !newWC.contains( WC.get(n) ))
							newWC.add( WC.get(n) );
					}
					else
					{
						WC.get(n).vf = 0;
					}
					
					//System.out.println("restant : " + (WC.get(n).courant));
				}
				else
				{
					WC.get(n).courant = WC.get(n).vf;
					if( !newWC.contains( WC.get(n) ))
						newWC.add( WC.get(n) );
				}

			}
			else
			{
				WC.get(n).courant = WC.get(n).vf;
				
				if( !newWC.contains( WC.get(n) ))
					newWC.add( WC.get(n) );
			}
		}
		
		
		for( int i = 0 ; i < newWC.size() ; i++ )
		{
			newWC.get(i).vf = newWC.get(i).courant;
			newWC.get(i).courant = 0;
		}
		
		WC = newWC;
	}
}
